package com.central.user.controller;

import com.central.model.common.PageResult;
import com.central.model.common.Result;
import com.central.user.model.ServerInfo;
import com.central.user.service.ServerServiceI;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//服务器管理
@RestController
public class ServerController {

    @Autowired
    private ServerServiceI serverServiceI;

    @PreAuthorize("hasAuthority('operation:get/server/getServerInfo')")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")})
    @GetMapping("/server/getServerInfo")
    public PageResult<ServerInfo> getsServerInfo(@RequestParam Map<String, Object> params) {
        return serverServiceI.getsServerInfo();
    }

    @PutMapping("/server/closeServer")
    public Result closeServer(String ip) {
        return serverServiceI.closeServer(ip);
    }

    @PutMapping("/server/startServer")
    public Result startServer(@RequestParam String ip) {
        return serverServiceI.startServer(ip);
    }

    @PutMapping("/server/getCode")
    public Result getCode(){
        String phone = SecurityContextHolder.getContext().
                getAuthentication().getPrincipal().toString().split(",")[5].split("=")[1];
        return serverServiceI.getCode(phone);
    }

    @PutMapping("/server/verifyCode")
    public Result verifyCode(@RequestParam int val){
        System.out.println(val);
        return serverServiceI.verifyCode(val);
    }


}
